package com.jzg.jcptadmin.Utils;

import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.Spinner;
import android.widget.TextView;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class UIUtils {

    public static final String TAG = UIUtils.class.getSimpleName();

    /**
     * 设置ListView高度
     *
     * @param listView
     */
    public static void setListViewHeight(ListView listView) {
        if (listView == null)
            return;
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return;
        }
        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }
        totalHeight+=500;
        LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight
                + (listView.getDividerHeight() * (listAdapter.getCount())-1);
        listView.setLayoutParams(params);
    }

    /**
     * 滚动到ScrollView顶部
     *
     * @param scrollView
     */
    public static void toScrollViewTop(ScrollView scrollView) {
        scrollView.smoothScrollTo(0, 20);
    }

    public static void setListViewHeightBasedOnChildren(GridView listView) {
        // 获取listview的adapter
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return;
        }
        // 固定列宽，有多少列
        int col = 6;// listView.getNumColumns();
        int totalHeight = 0;
        // i每次加4，相当于listAdapter.getCount()小于等于4时 循环一次，计算一次item的高度，
        // listAdapter.getCount()小于等于8时计算两次高度相加
        for (int i = 0; i < listAdapter.getCount(); i += col) {
            // 获取listview的每一个item
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            // 获取item的高度和
            totalHeight += listItem.getMeasuredHeight();
        }

        // 获取listview的布局参数
        LayoutParams params = listView.getLayoutParams();
        // 设置高度
        params.height = totalHeight;
        // 设置margin
        // ((MarginLayoutParams) params).setMargins(10, 10, 10, 10);
        // 设置参数
        listView.setLayoutParams(params);
    }

    /**
     * disableSubControls
     *
     * @param @param viewGroup
     * @return void
     * @throws
     * @Title: disableSubControls
     * @Description: 禁用ViewGroup中的控件
     */
    public static void disableSubControls(ViewGroup viewGroup) {
        for (int i = 0; i < viewGroup.getChildCount(); i++) {
            View v = viewGroup.getChildAt(i);

            if (v instanceof ViewGroup) {
                if (v instanceof Spinner) {
                    Spinner spinner = (Spinner) v;
                    spinner.setClickable(false);
                    spinner.setEnabled(false);

                    Log.i(TAG, "A Spinner is unabled");
                } else if (v instanceof ListView) {
                    ((ListView) v).setClickable(false);
                    ((ListView) v).setEnabled(false);

                    Log.i(TAG, "A ListView is unabled");
                } else {
                    disableSubControls((ViewGroup) v);
                }
            } else if (v instanceof EditText) {
                ((EditText) v).setEnabled(false);
                ((EditText) v).setClickable(false);

                Log.i(TAG, "A EditText is unabled");
            } else if (v instanceof Button) {
                ((Button) v).setEnabled(false);

                Log.i(TAG, "A Button is unabled");
            } else if (v instanceof TextView) {

                ((TextView) v).setEnabled(false);
                ((TextView) v).setClickable(false);
                Log.i(TAG, "A TextView is unabled");
            }
        }
        viewGroup.setEnabled(false);
        viewGroup.setClickable(false);
    }

    public static void openSubControls(ViewGroup viewGroup) {
        for (int i = 0; i < viewGroup.getChildCount(); i++) {
            View v = viewGroup.getChildAt(i);

            if (v instanceof ViewGroup) {
                if (v instanceof Spinner) {
                    Spinner spinner = (Spinner) v;
                    spinner.setClickable(true);
                    spinner.setEnabled(true);

                    Log.i(TAG, "A Spinner is unabled");
                } else if (v instanceof ListView) {
                    ((ListView) v).setClickable(true);
                    ((ListView) v).setEnabled(true);

                    Log.i(TAG, "A ListView is unabled");
                } else {
                    openSubControls((ViewGroup) v);
                }
            } else if (v instanceof EditText) {
                ((EditText) v).setEnabled(true);
                ((EditText) v).setClickable(true);

                Log.i(TAG, "A EditText is unabled");
            } else if (v instanceof Button) {
                ((Button) v).setEnabled(true);

                Log.i(TAG, "A Button is unabled");
            } else if (v instanceof TextView) {

                ((TextView) v).setEnabled(true);
                ((TextView) v).setClickable(true);
                Log.i(TAG, "A TextView is unabled");
            }
        }
    }

    public static String getUrl(Map<String,String> params){
        int index = 0;
        StringBuffer sb = new StringBuffer();
        Set<String> keys = params.keySet();
        Iterator<String> iterator = keys.iterator();
        while(iterator.hasNext()){
            String key = iterator.next();
            String value = params.get(key);
            if(index==0){
                sb.append("?").append(key).append("=").append(value);
            }else{
                sb.append("&").append(key).append("=").append(value);
            }
            index++;
        }
        return sb.toString();
    }

}
